<template>
  <div>
    <form action="/">
      <van-search
        v-model="value"
        show-action
        placeholder="请输入搜索关键词"
        @search="onSearch"
        @cancel="onCancel"
      />
    </form>

    <div
      v-show="isShow_list"
      v-for="value in mlist"
      :key="value.id"
      class="item"
      @click="play(value)"
    >
      <div class="item_div1">
        <img :src="value.albumPicUrl" />
      </div>
      <div class="item_div2">
        <p>专辑名称：{{ value.albumName }}</p>
        <p>音乐名称：{{ value.musicName }}</p>
        <p>歌手名称：{{ value.artistName }}</p>
        <p>类别：{{ value.navigationName }}</p>
        <!-- <audio :src="value.musicMp3Url" controls></audio> -->
      </div>
    </div>
    <h3 v-show="isShow">歌曲未搜索到</h3>
  </div>
</template>
<script>
import url from "../tools/url";

export default {
  name: "Search",
  data() {
    return {
      value: "",
      mlist: [],
      isShow: false,
      isShow_list: false,
    };
  },
  methods: {
    //   搜索歌曲
    onSearch() {
      var token = sessionStorage.getItem("token");
      var str = `token=${token}&keyword=${this.value}`;
      this.axios.post(url.FINDBYKEYWORD, str).then((res) => {
        if (res.data.code == 200) {
          if (res.data.data.length > 0) {
            this.mlist = res.data.data;
            this.isShow_list = true;
            this.isShow = false;
          } else {
            this.isShow_list = false;
            this.isShow = true;
          }
        } else {
          this.$router.push("/login");
        }
      });
    },
    // 取消搜索
    onCancel() {
      this.$router.go(-1);
    },
    // 播放歌曲
    play(v) {
      console.log(v);
      this.$store.commit("savem", v);
    },
  },
};
</script>
<style scoped>
p {
  font-size: 14px;
}
.item {
  display: flex;
  background: white;
  margin-top: 10px;
  border-radius: 0.5rem;
  box-shadow: 2px 2px 10px #ccc;
}
.item_div1 {
  width: 20%;
}
.item_div1 img {
  width: 100%;
  padding: 12px 10px;
  border-radius: 1rem;
}
.item_div2 {
  text-align: left;
  margin-left: 30px;
  width: 65%;
  line-height: 25px;
}
</style>
